<template>
  <div class="nav">
    <el-card>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原始密码" prop="oldPwd" style="width: 300px">
          <el-input  type="password" v-model.number="ruleForm.oldPwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd" style="width: 300px">
          <el-input
            type="password"
            v-model="ruleForm.newPwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass" style="width: 300px">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { editPwdReq, ChangePassword } from "@/api/user";
import { mapActions } from "vuex";

const checkOldPwd = async (rule, value, callback) => {
  if (!value) {
    callback(new Error("原始密码不能为空"));
  } else {
    let res = await ChangePassword({
      oldPwd: value,
    });
    let { code, msg } = res.data;
    if (code === 200) {
      // 3、正确
      callback();
    } else {
      callback(new Error(msg));
    }
  }
};

export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        newPwd: "",
        checkPass: "",
        oldPwd: "",
      },
      rules: {
        newPwd: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        oldPwd: [{ validator: checkOldPwd, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapActions("user", ["A_clear"]),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
         
          let res = await editPwdReq(this.ruleForm);
          let { code } = res.data;
          if (code === 0) {
            // 等待本地和vuex数据清空
            await this.A_clear();
            // 跳转
            this.$router.push("/login");
            // 刷新浏览器 重置路由
            location.reload();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields(this.ruleForm);
    },
  },
};
</script>

<style lang="scss" scoped>
.nav{
  margin: 20px 0px 0px 20px;
}
</style>